#include "ged.h"

int canFindGed(char*** fileList,int sizeL,char* file){
	int filelen = strlen(file);
	int result = 0;

	if( strstr(file+filelen-4, ".ged") != NULL)
		return 1;

	return result;
}


int main ( int argc , char **argv)
{
	char** list;
	int listsize;
	char** file;

/*	int fd = atoi(argv[2]);*/
	parseArgAndDupFiles(argc,argv,DUP_IN | DUP_OUT, NULL, NULL);
/*
	if(dup2(fd,STDOUT_FILENO) == -1){
		fprintf(stderr,"could not init file %d\n",fd);
		sleep(2);
		exit(EXIT_FAILURE);
	}
	fd = atoi(argv[1]);
	if(dup2(fd,STDIN_FILENO) == -1){
		fprintf(stderr,"could not init file %d\n",fd);
		sleep(2);
		exit(EXIT_FAILURE);

	}
	*/
	while ( 1 )
	{

		if(!askListToServer(stdin,&list,&listsize)){
			logMessage(stderr,"Server doesn't responded, try again\n");
			sleep(GED_DELAI);
			continue;
		}
		
		file = choseFile(&list,listsize,canFindGed);
		if(listsize == 0 || file == NULL || *file == NULL){
			sleep(GED_DELAI);
			continue;
		}

		if(sendOperation(stdin,stdout,OP_DEL,*file) == -1){
			logMessage(stderr,"It was not possible to remove the file %s\n",*file);
		}else{
			logMessage(stderr,"File succesfully removed and retrieved from server %s\n",*file);
		}
	}

	exit(EXIT_SUCCESS);
}
